*********************************
*****REPLICATION SCRIPT*********

***PAPER: "Drinking Tea with the Neighbors: Informal Clubs, General Trust, and Trustworthiness in Mali"

***AUTHORS: Jaimie Bleck, Jacopo Bonan, Philippe LeMay-Boucher and Bassirou Sarr

*** Data sources: 
*1. grin_indiv_data_APSR.dta: individual and group level characteristics for grin membership
*2. final_trust_surveyAPSR.dta: individual level characteristics for the whole study sample_1
*3. census_def_APSR2.dta: data for all individuals in the initial census 

********************************
*Table 1: grin characteristics
********************************
use grin_indiv_data_APSR2, clear

global c2  gform_7 elf_eth  memb_orig_1 memb_orig_2 d_econ_adv duration high_freq_meet d_leader member_entry_all teapayer_1  act_neigh   d_fin_help support_service contrib     

cap drop tag_group
egen tag_group=tag(group_id)
keep if tag_group==1
keep $c2
order $c2

outreg2 using "des_grin2c.xls", replace sum(detail) label

********************************
*Table 2: individual characteristics
********************************

use final_trust_surveyAPSR2.dta , clear

*panel A: Trust game features
sum treat*

bys d_sender : sum sh_tg_contr d_tg_contrib  d_tg_contrib_half
bys d_sender tg_treat  : sum sh_tg_contr d_tg_contrib  d_tg_contrib_half

foreach u in sh_tg_contr d_tg_contrib  d_tg_contrib_half{
reg `u' i.tg_treat if d_sender==1, robust 
reg `u' i.tg_treat if d_sender==0, robust 
}

*panel B: individual characteristics
local contr1 treat_1 treat_2 treat_3  env1 d_sender sh_tg_contr d_tg_contrib   d_tg_contrib_half

local contr2  tg_d_female tg_age minority_lang  in_couple  tg_educ_2 tg_educ_3 tg_educ_4 d_ec_act tg_d_riskaverse_small tg_lend_6 DG_base tg_grin tg_hh_size tg_north_hh  asset_index   d_mopti   

local contr `contr1' `contr2'

sum `contr1' `contr2'

bys tg_treat: sum `contr1' `contr2'

foreach u of local contr{
	reg `u' i.tg_treat , robust 
}

*panel C: grin members characteristics
local contr3 tg_transf_grin_amount  ecsup_pers_gr reas_part_cohe

sum `contr3' if tg_grin==1
bys tg_treat: sum `contr3' if tg_grin==1

foreach u of local contr3{
	reg `u' i.tg_treat , robust 
}

********************************
*Figure 1
********************************
use final_trust_surveyAPSR2.dta , clear

lab def tt 1 "Generalized" 2 "In-group" 3 "Out-group"
la val tg_treat tt

lab def gr 0 "Non-members" 1 "Grin members"
la val tg_grin gr

cibar sh_tg_contr if d_sender==1, over1( tg_grin tg_treat)  barcol(black  gs12) graphopts(title("Sender",) ytitle("TG Contribution %",) legend(on cols(3)) graphregion(color(white))  name(graph_contr11, replace)  )  


cibar sh_tg_contr if d_sender==0, over1(tg_grin tg_treat )  barcol(black  gs12) graphopts(title("Receiver",) ytitle("TG Contribution %",) legend(on cols(3)) graphregion(color(white))  name(graph_contr22, replace)  )  


********************************
*Table 3
********************************
use final_trust_surveyAPSR2.dta , clear

global c1  tg_d_female tg_age  in_couple  minority_lang tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index  tg_d_riskaverse_small tg_d_sav tg_lend_6 d_mopti  DG_base 

* Panel A: Sender
local cond1 "if d_sender==1"
local cond2 "if d_sender==1&treat_1==1"
local cond3 "if d_sender==1&treat_2==1"
local cond4 "if d_sender==1&treat_3==1"

local met0 "kernel"
local des_met0 "kernel"

foreach y in  d_tg_contrib sh_tg_contr d_tg_contrib_half{ 
forvalues k=1/4{
forvalues i=0/0{
cap drop pscore`k'
pscore tg_grin $c1  `cond`k'', logit level(0.001)  pscore(pscore`k') comsup 

		psmatch2 tg_grin  `cond`k'', pscore(pscore`k') outcome(`y') common `met`i''
		mhbounds `y'  `cond`k'', gamma(1(0.1)4)

}
}		
}	

* FDR q-values
foreach y in  d_tg_contrib sh_tg_contr d_tg_contrib_half{ 
mat drop _all

forvalues k=1/4{
forvalues i=0/0{
cap drop pscore`k'
pscore tg_grin $c1  `cond`k'', logit level(0.001)  pscore(pscore`k') comsup 
psmatch2 tg_grin  `cond`k'', pscore(pscore`k') outcome(`y') common `met`i''

local df=e(df_r)
local t= r(att) / r(seatt)

 local p_`y'_`k'= 2*ttail(`df',abs(`t'))
 
 di `p_`y'_`k''
 
 mat p_`y'_`k'=`p_`y'_`k''
 
mat pp_`y'=nullmat(pp_`y')\ p_`y'_`k'

preserve
drop _all
svmat pp_`y'
save pp_`y', replace
restore
 
}
}
}

foreach y in  d_tg_contrib sh_tg_contr d_tg_contrib_half{ 
use  pp_`y', clear

rename pp_`y'1 pval

pause on
set more off

* use "pvalues for sharpening.dta", clear

* Collect the total number of p-values tested

quietly sum pval
local totalpvals = r(N)

* Sort the p-values in ascending order and generate a variable that codes each p-value's rank

quietly gen int original_sorting_order = _n
quietly sort pval
quietly gen int rank = _n if pval~=.

* Set the initial counter to 1 

local qval = 1

* Generate the variable that will contain the BKY (2006) sharpened q-values

gen bky06_qval = 1 if pval~=.

* Set up a loop that begins by checking which hypotheses are rejected at q = 1.000, then checks which hypotheses are rejected at q = 0.999, then checks which hypotheses are rejected at q = 0.998, etc.  The loop ends by checking which hypotheses are rejected at q = 0.001.


while `qval' > 0 {
	* First Stage
	* Generate the adjusted first stage q level we are testing: q' = q/1+q
	local qval_adj = `qval'/(1+`qval')
	* Generate value q'*r/M
	gen fdr_temp1 = `qval_adj'*rank/`totalpvals'
	* Generate binary variable checking condition p(r) <= q'*r/M
	gen reject_temp1 = (fdr_temp1>=pval) if pval~=.
	* Generate variable containing p-value ranks for all p-values that meet above condition
	gen reject_rank1 = reject_temp1*rank
	* Record the rank of the largest p-value that meets above condition
	egen total_rejected1 = max(reject_rank1)

	* Second Stage
	* Generate the second stage q level that accounts for hypotheses rejected in first stage: q_2st = q'*(M/m0)
	local qval_2st = `qval_adj'*(`totalpvals'/(`totalpvals'-total_rejected1[1]))
	* Generate value q_2st*r/M
	gen fdr_temp2 = `qval_2st'*rank/`totalpvals'
	* Generate binary variable checking condition p(r) <= q_2st*r/M
	gen reject_temp2 = (fdr_temp2>=pval) if pval~=.
	* Generate variable containing p-value ranks for all p-values that meet above condition
	gen reject_rank2 = reject_temp2*rank
	* Record the rank of the largest p-value that meets above condition
	egen total_rejected2 = max(reject_rank2)

	* A p-value has been rejected at level q if its rank is less than or equal to the rank of the max p-value that meets the above condition
	replace bky06_qval = `qval' if rank <= total_rejected2 & rank~=.
	* Reduce q by 0.001 and repeat loop
	drop fdr_temp* reject_temp* reject_rank* total_rejected*
	local qval = `qval' - .001
}
	

quietly sort original_sorting_order
pause off
set more on

display "Code has completed."
display "Benjamini Krieger Yekutieli (2006) sharpened q-vals are in variable 'bky06_qval'"
display	"Sorting order is the same as the original vector of p-values"

save qq_`y', replace
}

* Panel : Receiver
use final_trust_surveyAPSR.dta , clear

global c1  tg_d_female tg_age  in_couple  minority_lang tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index  tg_d_riskaverse_small tg_d_sav tg_lend_6 d_mopti  DG_base 

local cond1 "if d_sender==0"
local cond2 "if d_sender==0&treat_1==1"
local cond3 "if d_sender==0&treat_2==1"
local cond4 "if d_sender==0&treat_3==1"

local met0 "kernel"
local des_met0 "kernel"

foreach y in  d_tg_contrib sh_tg_contr d_tg_contrib_half{ 
forvalues k=1/4{
forvalues i=0/0{
cap drop pscore`k'
pscore tg_grin $c1  `cond`k'', logit level(0.001)  pscore(pscore`k') comsup 

		psmatch2 tg_grin  `cond`k'', pscore(pscore`k') outcome(`y') common `met`i''
		mhbounds `y'  `cond`k'', gamma(1(0.1)4)

}
}		
}	


* FDR q-values
foreach y in  d_tg_contrib sh_tg_contr d_tg_contrib_half{ 
mat drop _all

forvalues k=1/4{
forvalues i=0/0{
cap drop pscore`k'
pscore tg_grin $c1  `cond`k'', logit level(0.001)  pscore(pscore`k') comsup 
psmatch2 tg_grin  `cond`k'', pscore(pscore`k') outcome(`y') common `met`i''

local df=e(df_r)
local t= r(att) / r(seatt)

 local p_`y'_`k'= 2*ttail(`df',abs(`t'))
 
 di `p_`y'_`k''
 
 mat p_`y'_`k'=`p_`y'_`k''
 
mat pp_`y'=nullmat(pp_`y')\ p_`y'_`k'

preserve
drop _all
svmat pp_`y'
save pp_`y'2, replace
restore
 
}
}
}

foreach y in  d_tg_contrib sh_tg_contr d_tg_contrib_half{ 
use  pp_`y'2, clear

rename pp_`y'1 pval

pause on
set more off

* Collect the total number of p-values tested

quietly sum pval
local totalpvals = r(N)

* Sort the p-values in ascending order and generate a variable that codes each p-value's rank

quietly gen int original_sorting_order = _n
quietly sort pval
quietly gen int rank = _n if pval~=.

* Set the initial counter to 1 

local qval = 1

* Generate the variable that will contain the BKY (2006) sharpened q-values

gen bky06_qval = 1 if pval~=.

while `qval' > 0 {
	* First Stage
	* Generate the adjusted first stage q level we are testing: q' = q/1+q
	local qval_adj = `qval'/(1+`qval')
	* Generate value q'*r/M
	gen fdr_temp1 = `qval_adj'*rank/`totalpvals'
	* Generate binary variable checking condition p(r) <= q'*r/M
	gen reject_temp1 = (fdr_temp1>=pval) if pval~=.
	* Generate variable containing p-value ranks for all p-values that meet above condition
	gen reject_rank1 = reject_temp1*rank
	* Record the rank of the largest p-value that meets above condition
	egen total_rejected1 = max(reject_rank1)

	* Second Stage
	* Generate the second stage q level that accounts for hypotheses rejected in first stage: q_2st = q'*(M/m0)
	local qval_2st = `qval_adj'*(`totalpvals'/(`totalpvals'-total_rejected1[1]))
	* Generate value q_2st*r/M
	gen fdr_temp2 = `qval_2st'*rank/`totalpvals'
	* Generate binary variable checking condition p(r) <= q_2st*r/M
	gen reject_temp2 = (fdr_temp2>=pval) if pval~=.
	* Generate variable containing p-value ranks for all p-values that meet above condition
	gen reject_rank2 = reject_temp2*rank
	* Record the rank of the largest p-value that meets above condition
	egen total_rejected2 = max(reject_rank2)

	* A p-value has been rejected at level q if its rank is less than or equal to the rank of the max p-value that meets the above condition
	replace bky06_qval = `qval' if rank <= total_rejected2 & rank~=.
	* Reduce q by 0.001 and repeat loop
	drop fdr_temp* reject_temp* reject_rank* total_rejected*
	local qval = `qval' - .001
}
	

quietly sort original_sorting_order
pause off
set more on

display "Code has completed."
display "Benjamini Krieger Yekutieli (2006) sharpened q-vals are in variable 'bky06_qval'"
display	"Sorting order is the same as the original vector of p-values"

save qq_`y'2, replace
}

********************************
*Tables 4-5: Correlation of trust game contribution and real world outcomes, sample of senders/receivers
********************************
use final_trust_surveyAPSR2.dta , clear

*Panels A-C
global c1  tg_d_female tg_age minority_lang  in_couple  tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index  tg_d_riskaverse_small tg_d_sav tg_lend_6 d_mopti DG_base  

global trust1  d_tg_vol_friends tg_h_vol_friends d_tg_vol_neigh tg_h_vol_neigh
global trust2  tg_trust_same_lang_sc tg_trust_north_sc tg_trust_otheth_sc tg_trust_othlang_sc tg_op_selfish   
global trust3  tg_wed_othlang tg_wed_othrel tg_wed_otheth

local cond1 "if d_sender==1"
local cond2 "if d_sender==0"
local c1 "sender"
local c2 "receiver"

forval i=1/2{
local replace replace

forval h=1/3{
foreach y of global trust`h'{

sum `y' `cond`i''
local avg_cont=r(mean)

reg `y' sh_tg_contr $c1 `cond`i'', vce(robust)
outreg2  using real_corr_`c`i''ww.xls,  `replace' label dec(3)  adds(Mean dep var, `avg_cont')
local replace append
test sh_tg_contr
matrix p_`i'_`h'=nullmat(p_`i'_`h')\(r(p))
}
}
}

****generate qvalues
forval i=1/2{

forval h=1/3{

matrix list p_`i'_`h'
drop _all
svmat p_`i'_`h'

rename p_`i'_`h' pval

pause on
set more off

quietly sum pval
local totalpvals = r(N)

* Sort the p-values in ascending order and generate a variable that codes each p-value's rank

quietly gen int original_sorting_order = _n
quietly sort pval
quietly gen int rank = _n if pval~=.

* Set the initial counter to 1 

local qval = 1

* Generate the variable that will contain the BKY (2006) sharpened q-values

gen bky06_qval = 1 if pval~=.

while `qval' > 0 {
	* First Stage
	* Generate the adjusted first stage q level we are testing: q' = q/1+q
	local qval_adj = `qval'/(1+`qval')
	* Generate value q'*r/M
	gen fdr_temp1 = `qval_adj'*rank/`totalpvals'
	* Generate binary variable checking condition p(r) <= q'*r/M
	gen reject_temp1 = (fdr_temp1>=pval) if pval~=.
	* Generate variable containing p-value ranks for all p-values that meet above condition
	gen reject_rank1 = reject_temp1*rank
	* Record the rank of the largest p-value that meets above condition
	egen total_rejected1 = max(reject_rank1)

	* Second Stage
	* Generate the second stage q level that accounts for hypotheses rejected in first stage: q_2st = q'*(M/m0)
	local qval_2st = `qval_adj'*(`totalpvals'/(`totalpvals'-total_rejected1[1]))
	* Generate value q_2st*r/M
	gen fdr_temp2 = `qval_2st'*rank/`totalpvals'
	* Generate binary variable checking condition p(r) <= q_2st*r/M
	gen reject_temp2 = (fdr_temp2>=pval) if pval~=.
	* Generate variable containing p-value ranks for all p-values that meet above condition
	gen reject_rank2 = reject_temp2*rank
	* Record the rank of the largest p-value that meets above condition
	egen total_rejected2 = max(reject_rank2)

	* A p-value has been rejected at level q if its rank is less than or equal to the rank of the max p-value that meets the above condition
	replace bky06_qval = `qval' if rank <= total_rejected2 & rank~=.
	* Reduce q by 0.001 and repeat loop
	drop fdr_temp* reject_temp* reject_rank* total_rejected*
	local qval = `qval' - .001
}
	

quietly sort original_sorting_order
pause off
set more on

display "Code has completed."
display "Benjamini Krieger Yekutieli (2006) sharpened q-vals are in variable 'bky06_qval'"
display	"Sorting order is the same as the original vector of p-values"

save q_values_`i'_`h', replace
}
}

mat drop _all

*Panel D
use grin_indiv_data_APSR2, clear

global trust_gr1 act_neigh d_tg_cont_econ  tg_cont_econ d_tg_cont_comm  tg_cont_comm 

local cond1 "if d_sender==1&tg_grin==1"
local cond2 "if d_sender==0&tg_grin==1"
local c1 "sender"
local c2 "receiver"

local replace replace
forval i=1/2{
forval h=1/1{
foreach y of global trust_gr`h'{

sum `y' `cond`i''
local avg_cont=r(mean)

reg `y' sh_tg_contr $c1 `cond`i'', vce(robust)
outreg2  using real_corr_`c`i''.xls, `replace' label dec(3) adds(Mean dep var, `avg_cont') 
local replace 
test sh_tg_contr
matrix pg_`i'_`h'=nullmat(pg_`i'_`h')\(r(p))
}
}
}

****generate qvalues
forval i=1/2{

forval h=1/1{

matrix list pg_`i'_`h'
drop _all
svmat pg_`i'_`h'

rename pg_`i'_`h' pval

pause on
set more off

quietly sum pval
local totalpvals = r(N)

* Sort the p-values in ascending order and generate a variable that codes each p-value's rank

quietly gen int original_sorting_order = _n
quietly sort pval
quietly gen int rank = _n if pval~=.

* Set the initial counter to 1 

local qval = 1

* Generate the variable that will contain the BKY (2006) sharpened q-values

gen bky06_qval = 1 if pval~=.

while `qval' > 0 {
	* First Stage
	* Generate the adjusted first stage q level we are testing: q' = q/1+q
	local qval_adj = `qval'/(1+`qval')
	* Generate value q'*r/M
	gen fdr_temp1 = `qval_adj'*rank/`totalpvals'
	* Generate binary variable checking condition p(r) <= q'*r/M
	gen reject_temp1 = (fdr_temp1>=pval) if pval~=.
	* Generate variable containing p-value ranks for all p-values that meet above condition
	gen reject_rank1 = reject_temp1*rank
	* Record the rank of the largest p-value that meets above condition
	egen total_rejected1 = max(reject_rank1)

	* Second Stage
	* Generate the second stage q level that accounts for hypotheses rejected in first stage: q_2st = q'*(M/m0)
	local qval_2st = `qval_adj'*(`totalpvals'/(`totalpvals'-total_rejected1[1]))
	* Generate value q_2st*r/M
	gen fdr_temp2 = `qval_2st'*rank/`totalpvals'
	* Generate binary variable checking condition p(r) <= q_2st*r/M
	gen reject_temp2 = (fdr_temp2>=pval) if pval~=.
	* Generate variable containing p-value ranks for all p-values that meet above condition
	gen reject_rank2 = reject_temp2*rank
	* Record the rank of the largest p-value that meets above condition
	egen total_rejected2 = max(reject_rank2)

	* A p-value has been rejected at level q if its rank is less than or equal to the rank of the max p-value that meets the above condition
	replace bky06_qval = `qval' if rank <= total_rejected2 & rank~=.
	* Reduce q by 0.001 and repeat loop
	drop fdr_temp* reject_temp* reject_rank* total_rejected*
	local qval = `qval' - .001
}
	

quietly sort original_sorting_order
pause off
set more on

display "Code has completed."
display "Benjamini Krieger Yekutieli (2006) sharpened q-vals are in variable 'bky06_qval'"
display	"Sorting order is the same as the original vector of p-values"

save qg_values_`i'_`h'bb, replace
}
}

**************************
***Supplementary Materials
**************************

********************************
*Table C1: Factors explaining grin membership
********************************
use census_def_APSR2, clear

reg d_grin d_male d_head eldest_son hh_size eth_bamb eth_peul d_couple d_educ_2 d_educ_3 d_educ_4 d_job tg_job_formal  d_bko, vce(cluster hhid)

*********************************
*Table D1: Trust and trustworthiness by membership and treatment
*********************************
use final_trust_surveyAPSR2.dta , clear

local cond1 "if d_sender==1&treat_1==1"
local cond2 "if d_sender==1&treat_2==1"
local cond3 "if d_sender==1&treat_3==1"

local cond4 "if d_sender==0&treat_1==1"
local cond5 "if d_sender==0&treat_2==1"
local cond6 "if d_sender==0&treat_3==1"

mat drop _all

foreach y in  d_tg_contrib  sh_tg_contr d_tg_contrib_half{ 
forval i=1/6{

sum `y' `cond`i'' & tg_grin==0
matrix `y'_`i'_0 = r(mean)

sum `y' `cond`i'' & tg_grin==1
matrix `y'_`i'_1 = r(mean)

matrix list `y'_`i'_0
matrix list  `y'_`i'_1

reg `y' tg_grin  `cond`i'', robust
 matrix b=e(b)
 matrix v=e(V)

local df= e(df_r)

local b=b[1,1]
di `b'
local se=sqrt(v[1,1])
di `se'
local t= `b'/`se'
di `t'
local p=2*ttail(`df', abs(`t') )
di `p'

matrix p_11= `p'
matrix list p_11


mat `y'_`i'= `y'_`i'_0 , `y'_`i'_1 , p_11

matrix list `y'_`i'

mat `y'= nullmat(`y'), `y'_`i'

}
}

foreach y in  d_tg_contrib  sh_tg_contr d_tg_contrib_half{ 

preserve
drop _all
svmat `y'

forval i=1/18{
format  `y'`i' %9.3f 
}

save des_`y', replace
restore
}


********************************
*Table E1: Propensity scores + Figure E1-E2: Density distribution of propensity scores by treatment sub-groups, senders/receivers
********************************
use final_trust_surveyAPSR2.dta , clear

global c1  tg_d_female tg_age  in_couple  minority_lang tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index  tg_d_riskaverse_small tg_d_sav tg_lend_6 d_mopti  DG_base

*sender
local cond1 "if d_sender==1"
local cond2 "if d_sender==1&treat_1==1"
local cond3 "if d_sender==1&treat_2==1"
local cond4 "if d_sender==1&treat_3==1"
local c1 "All"
local c2 "Generalized"
local c3 "In-group"
local c4 "Out-group"

local replace replace 
forvalues k=1/4{
forvalues i=0/0{
cap drop pscore`i'
pscore tg_grin $c1  `cond`k'', logit level(0.001)  pscore(pscore`i') comsup 
outreg2 using ps.xls,  label ctitle(Sender,`c`k'') `replace' dec(3)
local replace
psgraph, t(tg_grin) p(pscore`i') graphregion(color(white)) title(`c`k'')
graph save  score_sender_`k'.gph, replace
}
}

graph combine score_sender_1.gph score_sender_2.gph score_sender_3.gph score_sender_4.gph ,  ycommon graphregion(color(white))
graph save score_sender_all.pdf, replace


*receiver
local cond2 "if d_sender==0&treat_1==1"
local cond3 "if d_sender==0&treat_2==1"
local cond4 "if d_sender==0&treat_3==1"
local cond1 "if d_sender==0"
local c1 "All"
local c2 "Generalized"
local c3 "In-group"
local c4 "Out-group"

local replace  
forvalues k=1/4{
forvalues i=0/0{
cap drop pscore`i'
pscore tg_grin $c1  `cond`k'', logit level(0.001)  pscore(pscore`i') comsup 
outreg2 using ps.xls,  label ctitle(Receiver,`c`k'') `replace' dec(3)
local replace
psgraph, t(tg_grin) p(pscore`i') graphregion(color(white)) title(`c`k'')
graph save  score_rec_`k'.gph, replace
}
}

graph combine score_rec_1.gph score_rec_2.gph score_rec_3.gph score_rec_4.gph ,  ycommon graphregion(color(white))
graph save score_rec_all.gph, replace

********************************
*Table E2:  Matching quality
********************************
*Panel A: Sender
use final_trust_surveyAPSR2.dta , clear

global c1  tg_d_female tg_age  in_couple  minority_lang tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index  tg_d_riskaverse_small tg_d_sav tg_lend_6 d_mopti  DG_base

local cond1 "if d_sender==1"
local cond2 "if d_sender==1&treat_1==1"
local cond3 "if d_sender==1&treat_2==1"
local cond4 "if d_sender==1&treat_3==1"

local met0 "kernel"
local des_met0 "kernel"

mat drop _all
foreach y in  d_tg_contrib {  
forvalues k=1/4{
forvalues i=0/0{
cap drop pscore`k'
pscore tg_grin $c1  `cond`k'', logit level(0.001)  pscore(pscore`k') comsup 
	psmatch2 tg_grin  `cond`k'', pscore(pscore`k') outcome(`y') common `met`i''

pstest $c1  ,sum  t(tg_grin) both 

matrix qual_`k'_`y'= (r(meanbiasbef),r(meanbiasaft)\r(Bbef),r(Baft)\r(Rbef), r(Raft) )
matrix tot_`y'= nullmat(tot_`y'), qual_`k'_`y'
}
}
matrix list tot_`y'
matrix tot=nullmat(tot)\ tot_`y'
}
matrix list tot

drop _all
svmat tot
	
ge model=""
order model

replace model="Mean std bias" in 1
replace model="Rubin's B" in 2
replace model="Rubin's R" in 3

forval i=1/8{
format  tot`i' %9.1f 
}

rename tot1 All_bef
rename tot2 All_aft
rename tot3 Gen_bef
rename tot4 Gen_aft
rename tot5 In_bef
rename tot6 In_aft
rename tot7 Out_bef
rename tot8 Out_aft

save psm_qual_sender_dec22.dta,replace

*Panel B: receiver
use final_trust_surveyAPSR2.dta , clear

global c1  tg_d_female tg_age  in_couple  minority_lang tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index  tg_d_riskaverse_small tg_d_sav tg_lend_6 d_mopti DG_base  
local cond1 "if d_sender==0"
local cond2 "if d_sender==0&treat_1==1"
local cond3 "if d_sender==0&treat_2==1"
local cond4 "if d_sender==0&treat_3==1"

local met0 "kernel"
local des_met0 "kernel"

mat drop _all
foreach y in  d_tg_contrib { 
forvalues k=1/4{
forvalues i=0/0{
cap drop pscore`k'
pscore tg_grin $c1  `cond`k'', logit level(0.001)  pscore(pscore`k') comsup 
	psmatch2 tg_grin  `cond`k'', pscore(pscore`k') outcome(`y') common `met`i''

pstest $c1  ,sum  t(tg_grin) both 

matrix qual_`k'_`y'= (r(meanbiasbef),r(meanbiasaft)\r(Bbef),r(Baft)\r(Rbef), r(Raft) )
matrix tot_`y'= nullmat(tot_`y'), qual_`k'_`y'
}
}
matrix list tot_`y'
matrix tot=nullmat(tot)\ tot_`y'
}
matrix list tot

drop _all
svmat tot
	
ge model=""
order model

replace model="Mean std bias" in 1
replace model="Rubin's B" in 2
replace model="Rubin's R" in 3

forval i=1/8{
format  tot`i' %9.1f 
}

rename tot1 All_bef
rename tot2 All_aft
rename tot3 Gen_bef
rename tot4 Gen_aft
rename tot5 In_bef
rename tot6 In_aft
rename tot7 Out_bef
rename tot8 Out_aft

save psm_qual_receiver_dec22.dta,replace

********************************
*Table E3: PSM estimates, robustness with different matching methods
********************************
use final_trust_surveyAPSR2.dta , clear

global c1  tg_d_female tg_age  in_couple  minority_lang tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index  tg_d_riskaverse_small tg_d_sav tg_lend_6 d_mopti DG_base  

*Panel B: Sender
local cond1 "if d_sender==1"
local cond2 "if d_sender==1&treat_1==1"
local cond3 "if d_sender==1&treat_2==1"
local cond4 "if d_sender==1&treat_3==1"

local met1 "n(1)"
local des_met1 "nn matching"
local met2 "caliper(0.01) n(4)" 
local des_met2 "nn matching k=4, caliper 0.01"
local met3 "radius caliper(0.01)"
local des_met3 "radius, caliper 0.01"


foreach y in  d_tg_contrib sh_tg_contr d_tg_contrib_half{ 
forvalues k=1/3{
forvalues i=1/4{
cap drop pscore`k'
pscore tg_grin $c1  `cond`k'', logit level(0.001)  pscore(pscore`k') comsup 

		psmatch2 tg_grin  `cond`k'', pscore(pscore`k') outcome(`y') common `met`i''

}
}		
}	

*Panel B: Receiver
local cond1 "if d_sender==0"
local cond2 "if d_sender==0&treat_1==1"
local cond3 "if d_sender==0&treat_2==1"
local cond4 "if d_sender==0&treat_3==1"

local met1 "n(1)"
local des_met1 "nn matching"
local met2 "caliper(0.01) n(4)" 
local des_met2 "nn matching k=4, caliper 0.01"
local met3 "radius caliper(0.01)"
local des_met3 "radius, caliper 0.01"

foreach y in  d_tg_contrib sh_tg_contr d_tg_contrib_half{ 
forvalues k=1/4{
forvalues i=1/3{
cap drop pscore`k'
pscore tg_grin $c1  `cond`k'', logit level(0.001)  pscore(pscore`k') comsup 

		psmatch2 tg_grin  `cond`k'', pscore(pscore`k') outcome(`y') common `met`i''

}
}		
}	

********************************
*Table E4: PSM estimates of the impact of grin membership on trust and trustworthiness, sample restricted to individuals playing in the market
********************************
use final_trust_surveyAPSR2.dta , clear

global c1  tg_d_female tg_age  in_couple  minority_lang tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index  tg_d_riskaverse_small tg_d_sav tg_lend_6 d_mopti DG_base  

*Panel A: Sender
local cond1 "if d_sender==1"
local cond2 "if d_sender==1&treat_1==1"
local cond3 "if d_sender==1&treat_2==1"
local cond4 "if d_sender==1&treat_3==1"

local met0 "kernel"
local des_met0 "kernel"
local met1 "n(1)"
local des_met1 "nn matching"
local met2 "caliper(0.01) n(4)" 
local des_met2 "nn matching k=4, caliper 0.01"
local met3 "radius caliper(0.01)"
local des_met3 "radius, caliper 0.01"

foreach y in  d_tg_contrib sh_tg_contr d_tg_contrib_half{ 
mat drop _all

forvalues k=1/1{
forvalues i=0/3{
cap drop pscore`k'
pscore tg_grin $c1  `cond`k'' & env1==1, logit level(0.001)  pscore(pscore`k') comsup 


		psmatch2 tg_grin  `cond`k'' & env1==1, pscore(pscore`k') outcome(`y') common `met`i''
		
		local sample_size_`i'=e(N)
		di `sample_size_`i''
*generate matrix with att and se
		mat att_`k'_`i' = (r(att_`y'))\ (r(seatt_`y')) 
		local ttstat_`k'_`i'=abs(r(att_`y')/r(seatt_`y'))
		matrix tstat_`k'_`i'=(`ttstat_`k'_`i'' ) 
		
		sum _treated if _support==1
		local tr=r(mean)
		di `tr'
		sum _support
		local sup=r(mean)
		di `sup'
		local sample_size_aft_`i'= `sample_size_`i'' * `sup'
		di `sample_size_aft_`i''
		local n_treat=`tr' * `sample_size_aft_`i''
		local n_contr= `sample_size_aft_`i'' - `n_treat'
		sum `y' `cond`k''& tg_grin==0
		local mean_contr=r(mean)
				mat sample_size_`k'_`i' =(`n_treat', `n_contr', `sup', `mean_contr')\ (.,.,.,.)
				

local t90 = abs(invttail(`sample_size_`i'',0.95))
local t95 = abs(invttail(`sample_size_`i'',0.975))
local t99 = abs(invttail(`sample_size_`i'',0.995))


*generate column with # of stars for significance
			if tstat_`k'_`i'[1,1]>=`t99'{
				local star_`k'= 3
				}
			else if tstat_`k'_`i'[1,1]>=`t95'{
				local star_`k'= 2
				}
			else if tstat_`k'_`i'[1,1]>=`t90'{
				local star_`k'= 1
				}
			else {
				local star_`k'= 0
				}
			*matrix tstat_`i' =`tstat'
			matrix star_`k'_`i' = `star_`k''\(.)
	
		
		mat fin_`k'_`i'=  (att_`k'_`i'),(star_`k'_`i') 
				
		matrix glob_`i'= nullmat(glob_`i')\fin_`k'_`i'
		matrix sample_`i'=nullmat(sample_`i')\sample_size_`k'_`i'
		
		}
	}		
mat li glob_0
mat li glob_1
mat li glob_2
mat li glob_3
*mat li glob_4
*mat li glob_5

mat globC = sample_1 , glob_0, glob_1, glob_2 , glob_3
mat li globC

matrix glob= globC
*mat2txt, matrix(c4) saving (table 3) title(Panel C: Mean (SD) - Offer, disclosure and risk) append

drop _all
svmat glob
	
ge model=""
order model

replace model="All" in 1

forval i=3/12{
format  glob`i' %9.3f 
}

rename glob1 N_treat
rename glob2 N_control
rename glob3 share_on_support
rename glob4 Mean_control
rename glob5 ATT_kernel
rename glob7 ATT_NN1
rename glob9 ATT_NN4
rename glob11 Radius

save psm_grin_sender_`y'_mkt.dta,replace
use final_trust_surveyAPSR2.dta , clear
}

use psm_grin_sender_d_tg_contrib_mkt.dta,clear
use psm_grin_sender_sh_tg_contr_mkt.dta, clear
use psm_grin_sender_d_tg_contrib_half_mkt.dta, clear

*Panel B: Receiver

use final_trust_surveyAPSR2.dta , clear

global c1  tg_d_female tg_age  in_couple  minority_lang tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index  tg_d_riskaverse_small tg_d_sav tg_lend_6 d_mopti DG_base  

local cond1 "if d_sender==0"

local met0 "kernel"
local des_met0 "kernel"
local met1 "n(1)"
local des_met1 "nn matching"
local met2 "caliper(0.01) n(4)" 
local des_met2 "nn matching k=4, caliper 0.01"
local met3 "radius caliper(0.01)"
local des_met3 "radius, caliper 0.01"

foreach y in  d_tg_contrib sh_tg_contr d_tg_contrib_half{ 
mat drop _all

forvalues k=1/1{
forvalues i=0/3{
cap drop pscore`k'
pscore tg_grin $c1  `cond`k'' & env1==1, logit level(0.001)  pscore(pscore`k') comsup 

		psmatch2 tg_grin  `cond`k'' & env1==1, pscore(pscore`k') outcome(`y') common `met`i''
		
		local sample_size_`i'=e(N)
		di `sample_size_`i''
		mat att_`k'_`i' = (r(att_`y'))\ (r(seatt_`y')) 
		local ttstat_`k'_`i'=abs(r(att_`y')/r(seatt_`y'))
		matrix tstat_`k'_`i'=(`ttstat_`k'_`i'' ) 
		
		sum _treated if _support==1
		local tr=r(mean)
		di `tr'
		sum _support
		local sup=r(mean)
		di `sup'
		local sample_size_aft_`i'= `sample_size_`i'' * `sup'
		di `sample_size_aft_`i''
		local n_treat=`tr' * `sample_size_aft_`i''
		local n_contr= `sample_size_aft_`i'' - `n_treat'
		sum `y' `cond`k''& tg_grin==0
		local mean_contr=r(mean)
				mat sample_size_`k'_`i' =(`n_treat', `n_contr', `sup', `mean_contr')\ (.,.,.,.)
				

local t90 = abs(invttail(`sample_size_`i'',0.95))
local t95 = abs(invttail(`sample_size_`i'',0.975))
local t99 = abs(invttail(`sample_size_`i'',0.995))

*generate column with # of stars for significance
			if tstat_`k'_`i'[1,1]>=`t99'{
				local star_`k'= 3
				}
			else if tstat_`k'_`i'[1,1]>=`t95'{
				local star_`k'= 2
				}
			else if tstat_`k'_`i'[1,1]>=`t90'{
				local star_`k'= 1
				}
			else {
				local star_`k'= 0
				}
			*matrix tstat_`i' =`tstat'
			matrix star_`k'_`i' = `star_`k''\(.)
	
		
		mat fin_`k'_`i'=  (att_`k'_`i'),(star_`k'_`i') 
				
		matrix glob_`i'= nullmat(glob_`i')\fin_`k'_`i'
		matrix sample_`i'=nullmat(sample_`i')\sample_size_`k'_`i'
		
		}
	}		
mat li glob_0
mat li glob_1
mat li glob_2
mat li glob_3


mat globC = sample_1 , glob_0, glob_1, glob_2 , glob_3
mat li globC

matrix glob= globC
*mat2txt, matrix(c4) saving (table 3) title(Panel C: Mean (SD) - Offer, disclosure and risk) append

drop _all
svmat glob
	
ge model=""
order model

replace model="All" in 1

forval i=3/12{
format  glob`i' %9.3f 
}

rename glob1 N_treat
rename glob2 N_control
rename glob3 share_on_support
rename glob4 Mean_control
rename glob5 ATT_kernel
rename glob7 ATT_NN1
rename glob9 ATT_NN4
rename glob11 Radius

save psm_grin_receiv_`y'_mkt.dta,replace
use final_trust_surveyAPSR2.dta , clear
}

use psm_grin_receiv_d_tg_contrib_mkt.dta,clear
use psm_grin_receiv_sh_tg_contr_mkt.dta, clear
use psm_grin_receiv_d_tg_contrib_half_mkt.dta, clear

********************************
*Table F1 F2: Individual and group determinants of trust/trustworthiness (sender/receiver), sample of grin members
********************************
use grin_indiv_data_APSR2, clear

global c1 tg_d_female tg_age minority_lang  in_couple  tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index tg_d_riskaverse_small tg_d_sav tg_lend_6  ///
d_mopti DG_base reas_part_cohe tg_transf_grin_amount ecsup_pers_gr 
global c2  elf_eth  memb_orig_1 memb_orig_2 gform_7  
global c3 d_fin_help   d_econ_adv 
global c4 duration high_freq_meet   
global c5 teapayer_1 member_entry_all d_leader

local cond1 "if d_sender==1&tg_grin==1"
local cond2 "if d_sender==0&tg_grin==1"
local c1 "sender"
local c2 "receiver"

forval i=1/2{

local replace replace

foreach y in d_tg_contrib sh_tg_contr d_tg_contrib_half{

reg `y'  $c1 $c2 $c3 $c4 $c5   `cond`i'' , vce(robust)
outreg2  using determ_`c`i''_membersvv.xls, `replace' label dec(3)  
local replace

reg `y'  $c1 $c2 $c3 $c4 $c5   `cond`i'' & treat_1==1, vce(robust)
outreg2  using determ_`c`i''_membersvv.xls, `replace' label dec(3)  

reg `y'  $c1 $c2 $c3 $c4 $c5   `cond`i'' & treat_2==1, vce(robust)
outreg2  using determ_`c`i''_membersvv.xls, `replace' label dec(3)  

reg `y'  $c1 $c2 $c3 $c4 $c5  `cond`i'' & treat_3==1, vce(robust)
outreg2  using determ_`c`i''_membersvv.xls, `replace' label dec(3)  
}
}

********************************
*Table F3: Individual determinants of trust (sender), whole sample 
********************************
use final_trust_surveyAPSR2.dta , clear

global c1 tg_grin tg_d_female tg_age minority_lang  in_couple  tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index   d_mopti DG_base
 
local cond1 "if d_sender==1"
local c1 "sender"

local replace replace

forval i=1/1{

foreach y in d_tg_contrib sh_tg_contr d_tg_contrib_half{

reg `y'  $c1   `cond`i'' , vce(robust)
outreg2  using determ_`c`i''_all2.xls, `replace' label dec(3)  
local replace

reg `y'  $c1    `cond`i'' & treat_1==1, vce(robust)
outreg2  using determ_`c`i''_all2.xls, `replace' label dec(3)  

reg `y'  $c1   `cond`i'' & treat_2==1, vce(robust)
outreg2  using determ_`c`i''_all2.xls, `replace' label dec(3)  

reg `y'  $c1   `cond`i'' & treat_3==1, vce(robust)
outreg2  using determ_`c`i''_all2.xls, `replace' label dec(3)  
}
}

********************************
*Table F4: Individual determinants of trustworthiness (receiver), whole sample 
********************************
use final_trust_surveyAPSR.dta , clear

global c1 tg_grin endow_900  endow_600 tg_d_female tg_age minority_lang  in_couple  tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index   d_mopti DG_base

local cond2 "if d_sender==0"
local c2 "receiver"

local replace replace
forval i=2/2{

foreach y in d_tg_contrib sh_tg_contr d_tg_contrib_half{
reg `y'  $c1   `cond`i'' , vce(robust)
test endow_900= endow_600 
local pp=r(p)
outreg2  using determ_`c`i''_all3.xls, `replace' label dec(3)  adds(Pval equal, `pp')
local replace

reg `y'  $c1    `cond`i'' & treat_1==1, vce(robust)
test endow_900= endow_600 
local pp=r(p)
outreg2  using determ_`c`i''_all3.xls, `replace' label dec(3)  adds(Pval equal, `pp')

reg `y'  $c1   `cond`i'' & treat_2==1, vce(robust)
test endow_900= endow_600 
local pp=r(p)
outreg2  using determ_`c`i''_all3.xls, `replace' label dec(3)  adds(Pval equal, `pp')

reg `y'  $c1   `cond`i'' & treat_3==1, vce(robust)
test endow_900= endow_600 
local pp=r(p)
outreg2  using determ_`c`i''_all3.xls, `replace' label dec(3)  adds(Pval equal, `pp')
}
}

*************************************************
***Online appendix
**************************************************

********************************
*Table 1-4: Correlation of trust game contribution and real-world outcomes, sample of senders/receivers, full model
********************************

use final_trust_surveyAPSR2.dta , clear

*Panels A-C
global c1  tg_d_female tg_age minority_lang  in_couple  tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index  tg_d_riskaverse_small tg_d_sav tg_lend_6 d_mopti DG_base  

global trust1  d_tg_vol_friends tg_h_vol_friends d_tg_vol_neigh tg_h_vol_neigh
global trust2  tg_trust_same_lang_sc tg_trust_north_sc tg_trust_otheth_sc tg_trust_othlang_sc tg_op_selfish   
global trust3  tg_wed_othlang tg_wed_othrel tg_wed_otheth

local cond1 "if d_sender==1"
local cond2 "if d_sender==0"
local c1 "sender"
local c2 "receiver"

forval i=1/2{
local replace replace

forval h=1/3{
foreach y of global trust`h'{

sum `y' `cond`i''
local avg_cont=r(mean)

reg `y' sh_tg_contr $c1 `cond`i'', vce(robust)
outreg2  using real_corr_`c`i''ff.xls,  `replace' label dec(3)  adds(Mean dep var, `avg_cont')
local replace append

}
}
}

*Panel D
use grin_indiv_data_APSR2, clear

global trust_gr1 act_neigh d_tg_cont_econ  tg_cont_econ d_tg_cont_comm  tg_cont_comm 

local cond1 "if d_sender==1&tg_grin==1"
local cond2 "if d_sender==0&tg_grin==1"
local c1 "sender"
local c2 "receiver"

forval i=1/2{
foreach y of global trust_gr1{

sum `y' `cond`i''
local avg_cont=r(mean)

reg `y' sh_tg_contr  $c1 `cond`i'', vce(robust)
outreg2  using real_corr_`c`i''ff.xls,  `replace' label dec(3)  adds(Mean dep var, `avg_cont')

}
}

********************************
*Table 5-6: Correlation of trust game contribution and real-world outcomes, sample of senders, multiple outcomes
********************************
use final_trust_surveyAPSR2.dta , clear

*Panels A-C
global c1  tg_d_female tg_age minority_lang  in_couple  tg_educ_2 tg_educ_3 tg_educ_4 tg_hh_size tg_north_hh  d_ec_act asset_index  tg_d_riskaverse_small tg_d_sav tg_lend_6 d_mopti DG_base  

global trust1  d_tg_vol_friends tg_h_vol_friends d_tg_vol_neigh tg_h_vol_neigh
global trust2  tg_trust_same_lang_sc tg_trust_north_sc tg_trust_otheth_sc tg_trust_othlang_sc tg_op_selfish   
global trust3  tg_wed_othlang tg_wed_othrel tg_wed_otheth

local cond1 "if d_sender==1"
local cond2 "if d_sender==0"
local c1 "sender"
local c2 "receiver"


foreach x in  d_tg_contrib  d_tg_contrib_half{

forval i=1/2{
forval h=1/3{
foreach y of global trust`h'{

sum `y' `cond`i''
local avg_cont=r(mean)

reg `y' `x' $c1 `cond`i'', vce(robust)
outreg2  using real_corr_`c`i''_`x'dd.xls, `replace' label dec(3) adds(Mean dep var, `avg_cont') keep(`x')
}
}
}
}

*Panel D
use grin_indiv_data_APSR2, clear

global trust_gr1 act_neigh d_tg_cont_econ  tg_cont_econ d_tg_cont_comm  tg_cont_comm 

local cond1 "if d_sender==1&tg_grin==1"
local cond2 "if d_sender==0&tg_grin==1"
local c1 "sender"
local c2 "receiver"

foreach x in  d_tg_contrib  d_tg_contrib_half{

forval i=1/2{
foreach y of global trust_gr1{

sum `y' `cond`i''
local avg_cont=r(mean)

reg `y' `x' $c1 `cond`i'', vce(robust)
outreg2  using real_corr_`c`i''_`x'dd.xls, `replace' label dec(3) adds(Mean dep var, `avg_cont') keep(`x')

}
}
}


